home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MSG Demo 1.2 Source / MSG Demo ƒ / MSG Shell ƒ / msg main.c < prev    next >
C/C++ Source or Header  |  1993-11-10  |  5KB  |  212 lines

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. MSG Demo -- graphic effects demonstration program
  9. Copyright (C) 1992-3 Mark Pilgrim & Dave Blumenthal
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg menus.h"
  32. #include "msg sounds.h"
  33. #include "msg prefs.h"
  34. #include "msg environment.h"
  35. #include "demo.h"
  36. #include "AppleEvents.h"
  37. #include "EPPC.h"
  38.  
  39. void main(void)
  40. {
  41.     gDone = FALSE;    
  42.     MaxApplZone();    
  43.     InitGraf(&thePort);
  44.     InitFonts();
  45.     FlushEvents(everyEvent, 0);
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(0L);
  50.     InitCursor();
  51.     GetDateTime(&randSeed);
  52.     
  53.     CheckEnvironment();    
  54.     DoIntegrityCheck();    
  55.     InitSounds();
  56.     PrefsError(PreferencesInit());
  57.     InitEnvironment();    
  58.     InitProgram();
  59.     EventLoop();    
  60.     ShutDownEnvironment();
  61.     ExitToShell();
  62. }
  63.  
  64. void EventLoop(void)
  65. {
  66.     EventRecord        theEvent;
  67.     
  68.     while (!gDone)
  69.     {
  70.         SetCursor(&arrow);
  71.         HiliteMenu(0);
  72.         
  73.         if(gMainWindow)
  74.             SetPort(gMainWindow);
  75.         
  76.         if (WaitNextEvent(everyEvent, &theEvent, 30, 0L))
  77.             DispatchEvents(theEvent);
  78.     }
  79. }
  80.  
  81. void DispatchEvents(EventRecord gTheEvent)
  82. {
  83.     int                i;
  84.     OSErr            isHuman;
  85.     Point            thisPoint;
  86.     
  87.     switch (gTheEvent.what)
  88.     {
  89.         case mouseDown:
  90.             HandleMouseDown(gTheEvent);
  91.             break;
  92.         case keyDown:
  93.         case autoKey:
  94.             if(gTheEvent.modifiers & cmdKey)
  95.             {
  96.                 AdjustMenus();
  97.                 HandleMenu(MenuKey((char)(gTheEvent.message & charCodeMask)));
  98.             }
  99.             break;
  100.         case diskEvt:
  101.             if (HiWord(gTheEvent.message)!=noErr)
  102.             {
  103.                 DILoad();
  104.                 SetPt(&thisPoint, 120, 120);
  105.                 isHuman=DIBadMount(thisPoint, gTheEvent.message);
  106.                 DIUnload();
  107.             }
  108.             break;
  109.         case updateEvt:
  110.             BeginUpdate((WindowPtr)gTheEvent.message);
  111.             
  112.             if((WindowPtr)gTheEvent.message == gMainWindow)
  113.                 UpdateBoard();
  114.             
  115.             for (i=0; i<NUM_HELP; i++)
  116.                 if ((WindowPtr)gTheEvent.message == gHelp[i])
  117.                     UpdateHelp(i);
  118.             
  119.             EndUpdate((WindowPtr)gTheEvent.message);
  120.             break;
  121.         case kHighLevelEvent:
  122.             if (gHasAppleEvents)
  123.                 AEProcessAppleEvent(&gTheEvent);
  124.             break;
  125.     }
  126. }
  127.  
  128. void HandleMouseDown(EventRecord gTheEvent)
  129. {
  130.     WindowPtr    theWindow;
  131.     int            windowCode;
  132.     long        windSize;
  133.     GrafPtr        oldPort;
  134.     int            i;
  135.     
  136.     windowCode=FindWindow(gTheEvent.where, &theWindow);
  137.     switch (windowCode)
  138.     {
  139.         case inMenuBar:
  140.             AdjustMenus();
  141.             HandleMenu(MenuSelect(gTheEvent.where));
  142.             break;
  143.         case inContent:
  144.             if(FrontWindow() != theWindow)
  145.                 SelectWindow(theWindow);
  146.             break;
  147.         case inSysWindow:
  148.             SystemClick(&gTheEvent, theWindow);
  149.             break;
  150.         case inDrag:
  151.             DragWindow(theWindow, gTheEvent.where, &gDragRect);
  152.             if(theWindow == gMainWindow)
  153.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  154.             break;
  155.         case inGoAway:
  156.             if (TrackGoAway(theWindow, gTheEvent.where))
  157.             {
  158.                 for (i=0; i<NUM_HELP; i++)
  159.                     if (theWindow == gHelp[i])
  160.                         gHelp[i]=0L;
  161.                 
  162.                 if(theWindow == gMainWindow)
  163.                     CloseMainWindow();
  164.                 else
  165.                     DisposeWindow(theWindow);
  166.                 
  167.                 AdjustMenus();
  168.             }
  169.             break;
  170.         case inGrow:
  171.             gSizeRect = screenBits.bounds;
  172.             OffsetRect(&gSizeRect, gSizeRect.left, gSizeRect.top);
  173.             
  174.             windSize = GrowWindow(theWindow, gTheEvent.where, &gSizeRect);
  175.             if(windSize != 0)
  176.             {
  177.                 GetPort(&oldPort);
  178.                 SetPort(theWindow);
  179.                 EraseRect(&theWindow->portRect);
  180.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  181.                 InvalRect(&theWindow->portRect);
  182.                 SetPort(oldPort);
  183.             }
  184.             
  185.             if(theWindow == gMainWindow)
  186.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  187.             
  188.             break;
  189.         case inZoomIn:
  190.         case inZoomOut:
  191.             if(TrackBox(theWindow, gTheEvent.where, windowCode))
  192.             {
  193.                 GetPort(&oldPort);
  194.                 SetPort(theWindow);
  195.                 ZoomWindow(theWindow, windowCode, FALSE);
  196.                 InvalRect(&theWindow->portRect);
  197.                 SetPort(oldPort);
  198.             }
  199.             
  200.             if(theWindow == gMainWindow)
  201.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  202.             
  203.             break;
  204.     }
  205. }
  206.  
  207. void ShutDownEnvironment(void)
  208. {
  209.     ShutDownProgram();
  210.     CloseSounds();    
  211. }
  212.